/**
 * @Author: langchenglai
 * @Date:   2018-02-05 19:45:49
 * @Email:  lclangcheng@gmail.com
 * @Filename: base_component.js
 * @Last modified by:   lclangcheng
 * @Last modified time: 2018-10-22 17:05:19
 * @License: jin10
 * @Copyright: jin10
 */
'use strict'
const CustomError = require('../libs/custom_error')
const moment = require('moment')
const utils = require('../libs/utils')

class BaseComponent {
  /**
   * 创建错误对象
   * @param {Number} status
   * @param {String} message
   */
  createError (status = 400, message) {
    return new CustomError(status, message)
  }

  /**
   * 格式化日期
   * @param {Object} objRawObj
   */
  normalizeDate (objRawObj) {
    const obj = objRawObj
    if (!obj) {
      return null
    }

    for (let key of Object.keys(obj)) {
      if (moment.isDate(obj[key])) {
        obj[key] = utils.formatTime(obj[key])
      }
    }

    return obj
  }
}

module.exports = BaseComponent
